package com.example.aichat.controller;

import com.example.aichat.entity.Conversation;
import com.example.aichat.entity.Message;
import com.example.aichat.service.ChatService;
import com.example.aichat.common.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 聊天控制器
 */
@RestController
@RequestMapping("/api/chat")
public class ChatController {

    @Autowired
    private ChatService chatService;

    /**
     * 创建新对话
     */
    @PostMapping("/conversations")
    public Result<Conversation> createConversation(@RequestParam Long userId,
                                                  @RequestParam String title) {
        Conversation conversation = chatService.createConversation(userId, title);
        return Result.success(conversation);
    }

    /**
     * 发送消息
     */
    @PostMapping("/messages")
    public Result<Message> sendMessage(@RequestParam Long conversationId,
                                     @RequestParam String content,
                                     @RequestParam String sender) {
        Message message = chatService.sendMessage(conversationId, content, sender);
        return Result.success(message);
    }

    /**
     * 获取对话列表
     */
    @GetMapping("/conversations")
    public Result<List<Conversation>> getConversations(@RequestParam Long userId) {
        List<Conversation> conversations = chatService.getConversations(userId);
        return Result.success(conversations);
    }

    /**
     * 获取对话消息
     */
    @GetMapping("/messages")
    public Result<List<Message>> getMessages(@RequestParam Long conversationId) {
        List<Message> messages = chatService.getMessages(conversationId);
        return Result.success(messages);
    }

    /**
     * 删除对话
     */
    @DeleteMapping("/conversations/{conversationId}")
    public Result<?> deleteConversation(@PathVariable Long conversationId) {
        chatService.deleteConversation(conversationId);
        return Result.success();
    }
}    